﻿#include<iostream>

using namespace std;

#include"Date.h"

void Date::print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

bool Date::CheckDate()
{
	if (_month < 1 || _month > 12
		|| _day < 1 || _day > getmonthday(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}

Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= (-day);
	}
	_day += day;
	while (_day > getmonthday(_year, _month))
	{
		_day -= getmonthday(_year, _month);
		_month++;
		if (_month > 12)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day);
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month <= 0)
		{
			_month = 12;
			_year--;
		}
		_day += getmonthday(_year, _month);
	}
	return *this;
}

Date Date::operator-(int day)
{
	Date tem(*this);
	tem -= day;
	return tem;
}

Date Date::operator+(int day)
{
	Date tem(*this);
	tem += day;
	return tem;
}

Date& Date::operator++()
{
	return *this += 1;
}
Date Date::operator++(int)
{
	Date tem(*this);
	*this += 1;
	return tem;
}
Date& Date::operator--()
{
	return *this -= 1;
}
Date Date::operator--(int)
{
	Date tem(*this);
	*this -= 1;
	return tem;
}

bool Date::operator==(const Date& d) const
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

bool Date::operator<(const Date& d) const
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year && _month < d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day < d._day)
	{
		return true;
	}

	return false;
}

// d1 <= d2
bool Date::operator<=(const Date& d) const
{
	return *this < d || *this == d;
}

bool Date::operator>(const Date& d) const
{
	return !(*this <= d);
}

bool Date::operator>=(const Date& d) const
{
	return !(*this < d);
}

// 学习/解决问题 -> 减少走捷径
// d1 - d2
int Date::operator-(const Date& d) const
// 17:06
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;
}

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;

	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;

		if (d.CheckDate())
		{
			break;
		}
		else
		{
			cout << "非法日期，请重新输入!" << endl;
		}
	}

	return in;
}





